package com.example.city;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;

import com.example.city.CityPicker.testCity;

public class MainClickActivity extends Activity {

	private Button my_button_click;
	private TextView textView;
	private PopupWindow mCityPop,mDatePop;
	private View cityPopView,mDatePopView;
	private CityPicker cityPicker;
	//显示选择城市的 实时数据
	private static TextView sheng_Text;
	private static TextView shi_Text;
	private TextView Select_Ok,Select_Cancel;
	private static String mSheng,mShi,mquxian;
	public static String city_all="";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_click);
		my_button_click = (Button) findViewById(R.id.my_button_click);
		textView=(TextView) findViewById(R.id.my_text_city);
		selectionCityPOP(R.layout.select_city_pop_main_layout);
		my_button_click.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				backgroundAlpha(0.3f);
				showSelectionCityPOP(my_button_click);
			}
		});
	}
	//选择城市列表的 POP
		private void showSelectionCityPOP(View view) {
			if (!mCityPop.isShowing()) {
				mCityPop.showAtLocation(view, Gravity.BOTTOM, 0, 0);
			}
		}
		/**
		 * 选择城市 自定义控件
		 * 
		 * @param resId
		 */
		private void selectionCityPOP(int resId) {
			LayoutInflater mLayoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			cityPopView = mLayoutInflater.inflate(resId, null);
			mCityPop = new PopupWindow(cityPopView, LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT);
			// 必须设置background才能消失
			mCityPop.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.fullscreen_share_bg));
			mCityPop.setOutsideTouchable(true);
			// 自定义动画
//			mCityPop.setAnimationStyle(R.style.tudou_encrypt_dialog);
			// 使用系统动画
			mCityPop.setAnimationStyle(R.style.mypopwindow_anim_style);
			
			cityPicker=(CityPicker) cityPopView.findViewById(R.id.citypicker);
			
			sheng_Text=(TextView) cityPopView.findViewById(R.id.get_sheng);
			shi_Text=(TextView) cityPopView.findViewById(R.id.get_shi);
			Select_Ok=(TextView) cityPopView.findViewById(R.id.Select_City_Ok);
			Select_Cancel=(TextView) cityPopView.findViewById(R.id.Select_City_Cancel);
			
			sheng_Text.setText(cityPicker.getCity_string()+"");
			shi_Text.setText("");
//			quxian_Text.setText("");
			
			cityPicker.setCity(new testCity() {
				@Override
				public void cityAll(String sheng, String shi) {
					// TODO Auto-generated method stub
					mSheng=sheng;
					mShi=shi;
//					mquxian=quxian;
					handler.sendEmptyMessage(1);
				}
			});

			Select_Ok.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					city_all=mSheng+"  "+mShi;
					textView.setText(""+city_all);
					mCityPop.dismiss();
				}
			});
			
			Select_Cancel.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					city_all="";
					mCityPop.dismiss();
				}
			});
			// popWindow消失监听方法
			mCityPop.setOnDismissListener(new OnDismissListener() {
				@Override
				public void onDismiss() {
					backgroundAlpha(1f);
				}
			});
			
			mCityPop.update();
			mCityPop.setTouchable(true);
			mCityPop.setFocusable(true);
		}
		
		/**
		 * 设置添加屏幕的背景透明度
		 * @param bgAlpha
		 */
		public void backgroundAlpha(float bgAlpha){
			WindowManager.LayoutParams lp = getWindow().getAttributes();
			lp.alpha = bgAlpha; //0.0-1.0
	        getWindow().setAttributes(lp);
		}
		public static Handler handler=new Handler(){
			public void handleMessage(android.os.Message msg) {
				
				switch (msg.what) {
				case 1:
					sheng_Text.setText(mSheng+"");
					shi_Text.setText(mShi+"");
					break;
				default:
					break;
				}
			};
		};
		
}
